<?php

namespace app\admin\controller\live;

use app\common\controller\Backend;
use hehe\Str;
use think\facade\Db;

/**
 *
 */
class Detail extends Backend {
    /**
     * Goods模型对象
     * @var object
     * @phpstan-var \app\admin\model\goods\Goods
     */
    protected object $model;
    protected object $goodsModel;

    protected string|array $defaultSortField = 'id,desc';

    protected array|string $preExcludeFields = ['id', 'create_time'];

    protected string|array $quickSearchField = ['name', 'brand.name'];

    public function initialize(): void {
        parent::initialize();
        $this->goodsModel = new \app\admin\model\goods\Goods;
        $this->model = new \app\admin\model\live\Temp;
    }




    public function index(): void {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->param('select')) {
            $this->select();
        }

        $goods_ids = [];
        $param = $this->request->param();
        $live = Db::name('user_live')->where(['live_number' => $param['live_id']])->find();
        $live['create_time'] = date('m-d/H:i', $live['create_time']);
        $live['live_time'] = empty($live['live_time']) ? '' : date('m月d日 H点', $live['live_time']);
        $user = db::name('user')->where(['id' => $live['user_id']])->find();
        $live['user'] = $user;
        $result = Db::name('live_order_temp')->where(['live_id' => $live['id']])->select();
        foreach($result as $val) {
            $goods_ids[] = $val['goods_id'];
        }

        $where = [
            ['goods.id', 'in', $goods_ids]
        ];
        $list = $this->goodsModel->field($this->indexField)->with(['brand'])->alias('goods')->where($where)->select();



        foreach ($list as $key => $val) {
            $list[$key]['price'] = number_format($val['price'], 0, '.', ',');
            $list[$key]['supply_price'] = number_format($val['supply_price'], 0, '.', ',');
            $list[$key]['temp_id'] = $val['temp']['id'];
        }

        $this->success('', [
            'list' => $list,
            'remark' => get_route_remark(),
            'live' => $live
        ]);
    }




}